package com.caigou.modules.center.ui.detail.dynamic

import android.os.Bundle
import android.text.TextUtils
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.viewModels
import androidx.recyclerview.widget.DefaultItemAnimator
import androidx.recyclerview.widget.LinearLayoutManager
import com.alibaba.android.arouter.launcher.ARouter
import com.caigou.comm.base.bean.AppBackgroundInfo
import com.caigou.comm.base.constants.AppConstants
import com.caigou.comm.base.constants.RouterPath
import com.caigou.comm.base.container.BaseFragment
import com.caigou.comm.base.ext.Resource
import com.caigou.comm.base.ext.observe
import com.caigou.comm.base.ext.showToast
import com.caigou.comm.base.widget.SpaceItemDecoration
import com.caigou.modules.center.R
import com.caigou.modules.center.adapter.DynamicItemAdapter
import com.caigou.modules.center.bean.DynamicBean
import com.caigou.modules.center.databinding.FragmentForumDynamicBinding
import com.caigou.modules.center.ui.detail.DetailActivity
import com.caigou.modules.center.viewmodel.DetailViewModel
import dagger.hilt.android.AndroidEntryPoint

/**
 * @author #Suyghur.
 * Created on 2021/11/03
 */
@AndroidEntryPoint
class SingaporeTicketFragment : BaseFragment() {

    private var _binding: FragmentForumDynamicBinding? = null
    private val binding get() = _binding!!

    private val detailViewModel: DetailViewModel by viewModels()

    private var contentType = 0

    private val mAdapter: DynamicItemAdapter by lazy {
        DynamicItemAdapter(mutableListOf())
    }

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        _binding = FragmentForumDynamicBinding.inflate(inflater, container, false)
        return _binding?.root
    }

    override fun initView() {
        binding.forumDynamicSwipeRefreshLayout.run {
            setColorSchemeColors(resources.getColor(R.color.caigou_theme_color))
            setOnRefreshListener {
                fetchForumDynamicList()
            }
        }

        binding.forumDynamicRecyclerViewList.run {
            layoutManager = LinearLayoutManager(requireActivity())
            adapter = mAdapter
            itemAnimator = DefaultItemAnimator()
            addItemDecoration(SpaceItemDecoration(requireActivity()))
        }

        binding.forumDynamicTvBack.setOnClickListener {
            (requireActivity() as DetailActivity).onBackPressed()
        }
        mAdapter.setOnItemClickListener { _, _, position ->
            val item = mAdapter.getItem(position)
            ARouter.getInstance().build(RouterPath.Forum.PAGE_FORUM_POST)
                .withInt(AppConstants.POST_ID, item.dataId)
                .navigation()
        }


        binding.forumDynamicRgTypeSelector.setOnCheckedChangeListener { group, _ ->
            contentType = when (group.checkedRadioButtonId) {
                binding.forumDynamicBtnPublish.id -> 0
                binding.forumDynamicBtnReply.id -> 1
                else -> 0
            }
            fetchForumDynamicList()
        }
    }

    override fun lazyLoad() {
        fetchForumDynamicList()
    }

    override fun onBack() {
    }

    override fun observeViewModel() {
        observe(detailViewModel.forumDynamicLiveData, ::handleGetForumDynamicList)
    }

    override fun onDestroyView() {
        super.onDestroyView()
        _binding = null
    }

    private fun handleGetForumDynamicList(result: Resource<DynamicBean>) {
        when (result) {
            is Resource.Loading -> {
                mDialog.show()
            }
            is Resource.DataError -> {
                mDialog.dismiss()
                binding.forumDynamicSwipeRefreshLayout.isRefreshing = false
                if (!TextUtils.isEmpty(result.errorMsg)) {
                    showToast(result.errorMsg)
                } else {
                    showToast("获取我的动态信息异常")
                }
            }
            is Resource.Success -> {
                mDialog.dismiss()
                binding.forumDynamicSwipeRefreshLayout.isRefreshing = false
                if (result.data == null || result.data!!.list.isEmpty()) {
                    binding.forumDynamicLlEmptyComment.visibility = View.VISIBLE
                    binding.forumDynamicRecyclerViewList.visibility = View.GONE
                } else {
                    binding.forumDynamicLlEmptyComment.visibility = View.GONE
                    binding.forumDynamicRecyclerViewList.visibility = View.VISIBLE
                    mAdapter.setList(result.data!!.list)
                }
            }
        }
    }

    private fun fetchForumDynamicList() {
        detailViewModel.getForumDynamicList(3, AppBackgroundInfo.instance.userBean!!.userId, contentType)
    }
}